﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;
using System.Web;
using System.Drawing.Imaging;

namespace SDLITS.Web
{
    public class Finger
    {
        /// <summary>
        /// 是否正确连接
        /// </summary>
        /// <returns></returns>
        [DllImport("MXOTDLL.dll", EntryPoint = "IsMxUsbDevice",
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 IsMxUsbDevice();

        //[DllImport("MXOTDLL.dll", EntryPoint = "IsMxGetImage",
        //    CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        //public static extern Int32 IsMxGetImage();

        /// <summary>
        /// 从指纹仪中，获取指纹图像
        /// </summary>
        /// <param name="imageBuf"></param>
        /// <param name="nTimeOut"></param>
        /// <returns></returns>
        [DllImport("MXOTDLL.dll", EntryPoint = "mxUsbGetImage",
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 mxUsbGetImage(byte[] imageBuf, long nTimeOut);






        [DllImport("MXOTDLL.dll", EntryPoint = "MXReleaseDev",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 MXReleaseDev();


        [DllImport("MXOTDLL.dll", EntryPoint = "MXIsFingerOn",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXIsFingerOn();

        [DllImport("MXOTDLL.dll", EntryPoint = "MXDetectFinger",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXDetectFinger(byte[] FingerBuf);



        [DllImport("MXOTDLL.dll", EntryPoint = "MXExtract",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXExtract(byte[] FingerBuf, byte[] FeatureBuf);


        [DllImport("MXOTDLL.dll", EntryPoint = "MXSaveFeatureToFile",
CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXSaveFeatureToFile(byte[] FeatureBuf, string strFeatureName);



        [DllImport("MXOTDLL.dll", EntryPoint = "MXInitDev",
   CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXInitDev(object s);


        /// <summary>
        /// 从传感器上读入指纹图像并存放到 FingerBuf 中
        /// </summary>
        /// <param name="FingerBuf"></param>
        /// <returns></returns>
        [DllImport("MXOTDLL.dll", EntryPoint = "MXReadFingerFromSensor",
        CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXReadFingerFromSensor(byte[] FingerBuf);



        [DllImport("MXOTDLL.dll", EntryPoint = "MXSaveFingerToFile",
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Boolean MXSaveFingerToFile(byte[] FingerBuf, string strFingerName);



        [DllImport("MXOTDLL.dll", EntryPoint = "mxRaw2Bmp",
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 mxRaw2Bmp(byte[] pBmp, byte[] pRaw, int x, int y);


        [DllImport("MXOTDLL.dll", EntryPoint = "mxBmp2Raw",
          CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 mxBmp2Raw(byte[] pBmp, byte[] pRaw, int x, int y);


        [DllImport("MXOTDLL.dll", EntryPoint = "IsMxGetImage",
          CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 IsMxGetImage();

        [DllImport("MXOTDLL.dll", EntryPoint = "MXLoadFingerFromFile",
            CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        public static extern Int32 MXLoadFingerFromFile(string strFingerName, byte[] FingerBuf);


        public static string GetFinger(string IDNumber)
        {
            
            Bitmap b1 = new Bitmap((HttpContext.Current.Server.MapPath("~/img/fingertemp/finger.bmp")));
            MemoryStream ms1 = new MemoryStream();
            b1.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] FingerBuf = ms1.GetBuffer();
            byte[] FingerBuf1 = new byte[304 * 256];
            int ss = Finger.mxUsbGetImage(FingerBuf1, 0);
            Finger.mxRaw2Bmp(FingerBuf, FingerBuf1, 256, 304);
            //foreach (var item in FingerBuf)
            //{
            //    Response.Write(item);
            //}
            MemoryStream ms = new MemoryStream(FingerBuf);
            Image image = Image.FromStream(ms);
            string temppath = "/Img/FingerShotTemp/" + DateTime.Now.ToString("yyyyMMddHHmmss") + IDNumber + ".bmp";
            image.Save(HttpContext.Current.Server.MapPath("~/Img/FingerShot/" + IDNumber + ".bmp"), ImageFormat.Bmp);
            image.Save(HttpContext.Current.Server.MapPath("~/" + temppath), ImageFormat.Bmp);
            image.Dispose();
            b1.Dispose();
            return temppath;
        }


    }
}